package com.dingreading.cloud.web.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.aliyun.CdnUtil;
import com.dingreading.cloud.web.entity.StudyVideo;
import com.dingreading.cloud.web.entity.StudyVideoExpire;
import com.dingreading.cloud.web.entity.StudyVideoLog;
import com.dingreading.cloud.web.entity.table.StudyVideoTableDef;
import com.dingreading.cloud.web.mapper.StudyVideoMapper;
import com.dingreading.cloud.web.service.StudyVideoExpireService;
import com.dingreading.cloud.web.service.StudyVideoLogService;
import com.dingreading.cloud.web.service.StudyVideoService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2025-09-17
 */
@RefreshScope
@Service
public class StudyVideoServiceImpl extends BaseServiceImpl<StudyVideoMapper, StudyVideo, StudyVideoTableDef> implements StudyVideoService {

    @Override
    protected StudyVideoTableDef getTable() {
        return StudyVideoTableDef.studyVideo;
    }

    @Value("${aliYun.cdn.cdnDomain}")
    private String cdnDomain;
    @Value("${aliYun.cdn.privateKey}")
    private String cdnPrivateKey;

    @Resource
    private StudyVideoExpireService studyVideoExpireService;
    @Resource
    private StudyVideoLogService studyVideoLogService;


    @Override
    public StudyVideo getByUid(String uid) {
        return getMapper().selectOneByCondition(table.uid.eq(uid));
    }

    @Override
    public R<Object> getCdnUrl(String uid, String ip) throws Exception {
        StudyVideoExpire studyVideoExpire = studyVideoExpireService.getByUid(uid);
        if (studyVideoExpire == null) {
            return R.fail("未查询到视频");
        }
        if (!studyVideoExpire.getEnabled().equals(1)) {
            return R.fail("视频已被禁用");
        }
        StudyVideo studyVideo = getByUid(studyVideoExpire.getVideoUid());
        if (studyVideo == null) {
            return R.fail("未查询到原视频");
        }
        if (!studyVideo.getEnabled().equals(1)) {
            return R.fail("原视频已被禁用");
        }

        long l = studyVideoLogService.todayViewCount(uid, ip);
        if (l >= 50) {
            return R.fail("当日访问次数已超过50次");
        }

        Date expireTime = studyVideoExpire.getExpireTime();
        String expire = String.valueOf(expireTime.getTime());
        String url = studyVideo.getUrl();
        String token = CdnUtil.getToken(url, ip, expire, cdnPrivateKey);
        String cdnUrl = cdnDomain + url + "?token=" + token;

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("cdnUrl", cdnUrl);
        jsonObject.put("name", studyVideo.getName());

        // 保存获取记录
        StudyVideoLog log = new StudyVideoLog();
        log.setVideoUid(studyVideo.getUid());
        log.setVideoExpireUid(uid);
        log.setIp(ip);
        log.setName(studyVideo.getName());
        log.setExpireTime(expireTime);
        studyVideoLogService.save(log);

        return R.ok(jsonObject);
    }
}
